<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */

// no direct access
defined( '_VALID_MOS' ) or die();


//$view = isset($_GET['view']) ? intval($_GET['view']) : 2;
$view = intval(mosGetParam($_GET, 'view', 2))  ;
if ($view < '2') {
	setCookie('forum_view', $view, time()+31536000, '/');
	header("Location: ".$_SERVER["HTTP_REFERER"]);
}else {
	if (!defined('YARBB'))
		exit;

	if ($yarbb_user['g_post_topics'] == '1') {
		$post_link = "\t\t".'<p class="postlink conr"><a href="'.sefRelToAbs(YARBB_URL.'&amp;task=post&amp;all=1').'">'._Y_YARNEWTOP.'</a></p>'."\n";
	}
	else {
		$sql = ('SELECT COUNT(forum_id)
					FROM #__forum_forum_perms 
					WHERE group_id='.$yarbb_user['g_id'].' 
					AND post_topics=1');
		$database->setQuery($sql);
		$post_topics = $database->loadResult();
		$post_topics = $post_topics[0];
		if ($post_topics > 0) {
			$post_link = "\t\t".'<p class="postlink conr"><a href="'.sefRelToAbs(YARBB_URL.'&amp;task=post&amp;all=1').'">'._Y_YARNEWTOP.'</a></p>'."\n";
		}
		else {
			$post_link = '';
		}
	}
	$sql = ('SELECT SUM(f.num_topics)
							FROM #__forum_forums AS f 
							LEFT JOIN #__forum_forum_perms AS fp 
							ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].') 
							WHERE (fp.read_forum IS NULL OR fp.read_forum=1)');
	$database->setQuery($sql);
	$total_num_topics = $database->loadResult();
	//$total_num_topics = $total_num_topics[0];

	// Determine the topic offset (based on $_GET['p'])
	$num_pages = ceil($total_num_topics / $yarbb_user['disp_topics']);

	$p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p'];
	$start_from = $yarbb_user['disp_topics'] * ($p - 1);

	// Generate paging links
	$paging_links = _PN_PAGE.': '.paginate($num_pages, $p, sefRelToAbs( YARBB_URL.'&amp;task=index'));
	$mainframe->setPageTitle( yarbb_htmlspecialchars($yarbb_config['o_board_title']) );

	define('YARBB_ALLOW_INDEX', 1);
	$rate_topic = 0;
	$rate_stars = 0;
//Check if Rate Topic is installed and enabled.
//if (isset ($yarbb_config['o_rt_cur_version']) && $yarbb_config['o_rt_enabled'])
//{
	$rate_topic = 1;
	/*//Check for language pack.
	if (file_exists (YARBB_ROOT . 'lang/' . $yarbb_user['language'] . '/rate_topic.php'))
		require YARBB_ROOT . 'lang/' . $yarbb_user['language'] . '/rate_topic.php';
	else if (file_exists (YARBB_ROOT . 'lang/' . $yarbb_config['o_rt_default_lang'] . '/rate_topic.php'))
		require YARBB_ROOT . 'lang/' . $yarbb_config['o_rt_default_lang'] . '/rate_topic.php';
	else
		$rate_topic = 0;*/
	//Check if using stars or numbers.
	/*if ($yarbb_config['o_rt_use_stars'])
		if (file_exists ($yarbb_path . '/images/rate_stars/' . $yarbb_config['o_rt_cur_stars']))
			$rate_stars = 1;*/
//}
	?>
<div class="linkst">
	<div class="inbox">
		<p class="pagelink conl"><?php echo $paging_links ?></p>
	<?php echo $post_link ?>
		<ul><li><a href="<?php echo sefRelToAbs(YARBB_URL) ?>"><?php echo _Y_INDEX; ?></a>&nbsp;</li><li>&raquo;&nbsp;<?php echo _Y_YARALLTOP; ?></li></ul>
	</div>
</div>
<div id="vf" class="blocktable">
	<h2><span><?php echo _Y_YARALLTOP; ?></span></h2>
	<div class="box">
		<div class="inbox">
			<table cellspacing="0">
				<thead>
					<tr>
						<th class="tcl" scope="col"><?php echo _Y_TOPICS; ?></th>
						<th class="tcl" scope="col"><?php echo _Y_RSSFORUM; ?>&nbsp;</th>
						<th class="tc3" scope="col"><?php echo _Y_REPLYSY; ?>&nbsp;</th>
						<th class="tc3" scope="col"><?php echo _Y_YARVIEWTOP; ?>&nbsp;</th>
						<th class="ag_tcr" scope="col"><?php echo _Y_MESSAGE; ?></th>
					</tr>
				</thead>
				<tbody>
	<?php
// Fetch list of topics to display on this page
	if ($yarbb_user['is_guest'] || $yarbb_config['o_show_dot'] == '0') {
		// Without "the dot"
							$sql = 'SELECT f.forum_name, t.forum_id, t.id, t.poster, t.subject, descrip_t, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.icon_topic
					FROM #__forum_topics AS t 
					INNER JOIN #__forum_forums AS f 
					ON f.id=t.forum_id 
					LEFT JOIN #__forum_forum_perms AS fp 
					ON (fp.forum_id=t.forum_id 
					AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
					ORDER BY sticky DESC, last_post 
					DESC LIMIT '.$start_from.', '.$yarbb_user['disp_topics'];
						}
						else {
							// With "the dot"
							switch ($db_type) {
								case 'mysql':
								case 'mysqli':
									$sql = 'SELECT f.forum_name, p.poster_id AS has_posted, t.forum_id, t.id, t.subject, t.descrip_t, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, t.icon_topic
					FROM #__forum_topics AS t 
					INNER JOIN #__forum_forums AS f ON f.id=t.forum_id 
					LEFT JOIN #__forum_posts AS p ON t.id=p.topic_id 
					AND p.poster_id='.$yarbb_user['id'].' 
					LEFT JOIN #__forum_forum_perms AS fp 
					ON (fp.forum_id=t.forum_id AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) GROUP BY t.id 
					ORDER BY sticky DESC, last_post 
					DESC LIMIT '.$start_from.', '.$yarbb_user['disp_topics'];
									break;
								case 'sqlite':
									$sql = 'SELECT f.forum_name, p.poster_id AS has_posted, t.forum_id, t.id, t.subject, t.descrip_t, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, t.icon_topic
					FROM #__forum_topics AS t 
					INNER JOIN #__forum_forums AS f ON f.id=t.forum_id 
					LEFT JOIN #__forum_posts AS p ON t.id=p.topic_id AND p.poster_id='.$yarbb_user['id'].' 
					LEFT JOIN #__forum_forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) ORDER BY sticky DESC, last_post 
					DESC LIMIT '.$start_from.', '.$yarbb_user['disp_topics'].' GROUP BY t.id 
					ORDER BY t.sticky DESC, t.last_post DESC';
									break;
								default:
									$sql = 'SELECT f.forum_name, p.poster_id AS has_posted, t.forum_id, t.id, t.subject, t.descrip_t, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, t.icon_topic
					FROM #__forum_topics AS t 
					INNER JOIN #__forum_forums AS f ON f.id=t.forum_id 
					LEFT JOIN #__forum_posts AS p ON t.id=p.topic_id AND p.poster_id='.$yarbb_user['id'].' 
					LEFT JOIN #__forum_forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$yarbb_user['g_id'].') 
					WHERE (fp.read_forum IS NULL OR fp.read_forum=1) 
					GROUP BY t.forum_id, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, p.poster_id, f.forum_name 
					ORDER BY sticky DESC, last_post 
					DESC LIMIT '.$start_from.', '.$yarbb_user['disp_topics'];
									break;
							}
						}
						$database->setQuery($sql);
						$cur_topic_result_1 = $database->loadAssocList();
//$cur_topic_result_1 = $cur_topic_result_1[0];
// If there are topics in this forum.
						if ($cur_topic_result_1) {
							foreach ($cur_topic_result_1 as $cur_topic) {
								$item_status = '';
								$icon_type = 'icon';
								if ($cur_topic['moved_to'] == null) { //Easy Poll
									if ($cur_topic['question'] != '')     //Easy Poll
										$last_post = 'Re: <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$cur_topic['last_post_id'].'#p'.$cur_topic['last_post_id']).'">'.yarbb_htmlspecialchars($cur_topic['subject']).'<br /></a>'.format_time($cur_topic['last_post']).'<br /><span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['last_poster']).'</span>';  //Easy Poll
									else        //Easy Poll
										$last_post = 'Re: <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$cur_topic['last_post_id'].'#p'.$cur_topic['last_post_id']).'">'.yarbb_htmlspecialchars($cur_topic['subject']).'<br /></a>'.format_time($cur_topic['last_post']).'<br /><span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['last_poster']).'</span>';
								}             //Easy Poll
								else{
									$last_post = '&nbsp;';
								}

								if ($cur_topic['question'] != '') {

									if ($cur_topic['moved_to'] != 0)
										$subject = _Y_FRONTMOVED.' <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['moved_to']).'">'.yarbb_htmlspecialchars($cur_topic['question']).'</a><br /> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';
									else if ($cur_topic['closed'] == '0')
										$subject = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['id']).'">'.yarbb_htmlspecialchars($cur_topic['question']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';
									else {
										$subject = _Y_YARPOLLFRONT.'<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['id']).'">'.yarbb_htmlspecialchars($cur_topic['question']).'</a><br /><span class="byuser"><b>'.yarbb_htmlspecialchars($cur_topic['subject']).'</b> '._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';

										$item_status = 'iclosed';
									}
									if (!$yarbb_user['is_guest'] && $cur_topic['last_post'] > $yarbb_user['last_visit'] && $cur_topic['moved_to'] == null) {
										$item_status .= 'inew';
										$icon_type = 'icon inew';
										$subject = '<strong>'.$subject.'</strong>';
										$subject_new_posts = '<span class="newtext">&nbsp;<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$cur_topic['last_post_id'].'#p'.$cur_subforum['last_post_id']).'"  title="'._Y_YAGOTONEWTOP.'"><img src="'.$yarbb_live.'/style/images/'.$yarbb_config['o_default_style'].'/folder/folder_new_hot.gif" alt="'._Y_YARNEWPOSTS22.'"/></a>&nbsp;</span>';
									}
									else
										$subject_new_posts = null;
									// Should we display the dot or not? :)
									if (!$yarbb_user['is_guest'] && $yarbb_config['o_show_dot'] == '1') {
										if ($cur_topic['has_posted'] == $yarbb_user['id'])
											$subject = '<strong>&middot;</strong>&nbsp;'.$subject;
										else
											$subject = '&nbsp;&nbsp;'.$subject;
									}
								} else {
									if ($cur_topic['moved_to'] != 0)
										$subject = _Y_FRONTMOVED.': <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['moved_to']).'">'.yarbb_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';
									else if ($cur_topic['closed'] == '0')
										$subject = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['id']).'">'.yarbb_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';
									else {
										$subject = '<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['id']).'">'.yarbb_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'._Y_YARBY.'&nbsp;'.yarbb_htmlspecialchars($cur_topic['poster']).'</span>';
										$item_status = 'iclosed';
									}
									if (!$yarbb_user['is_guest'] && $cur_topic['last_post'] > $yarbb_user['last_visit'] && $cur_topic['moved_to'] == null) {
										$item_status .= ' inew';
										$icon_type = 'icon inew';
										$subject = '<strong>'.$subject.'</strong>';
										$subject_new_posts = '<span class="newtext">&nbsp;<a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$cur_topic['last_post_id'].'#p'.$cur_subforum['last_post_id']).'" title="'._Y_YAGOTONEWTOP.'"><img src="'.$yarbb_live.'/style/images/'.$yarbb_config['o_default_style'].'/folder/folder_new_hot.gif" alt="'._Y_YARNEWPOSTS22.'"/></a>&nbsp;</span>';
									}
									else
										$subject_new_posts = null;
									// Should we display the dot or not? :)
									if (!$yarbb_user['is_guest'] && $yarbb_config['o_show_dot'] == '1') {
										if ($cur_topic['has_posted'] == $yarbb_user['id'])
											$subject = '<strong>&middot;</strong>&nbsp;'.$subject;
										else
											$subject = '&nbsp;&nbsp;'.$subject;
									}
								}
								if ($cur_topic['sticky'] == '1') {
									$subject = '<span class="stickytext">'._Y_YARSTIKY.': </span>'.$subject;
									$item_status .= ' isticky';
								}
								if ($cur_topic['icon_topic'] != 0) {
									$icon_topic = '<img src="components/com_yarbb/images/icons/'.$cur_topic['icon_topic'].'.gif" alt="" />';
									$subject = $icon_topic.' '.$subject;
								}
								$num_pages_topic = ceil(($cur_topic['num_replies'] + 1) / $yarbb_user['disp_posts']);
								if ($num_pages_topic > 1)
									$subject_multipage = '[ '.paginate($num_pages_topic, -1, sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;id='.$cur_topic['id'])).' ]';
								else
									$subject_multipage = null;
								// Should we show the "New posts" and/or the multipage links?
								if (!empty($subject_new_posts) || !empty($subject_multipage)) {
									$subject .= '&nbsp; '.(!empty($subject_new_posts) ? $subject_new_posts : '');
									$subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
									$rowbg = $rowbg == 'even' ? 'odd' : 'even';
								}
//=========================
								//=====RATE TOPIC CODE=====
								if ($rate_topic) {
									//Select the rating and user_id for all ratings of the current topic.
									$sql = $db->query ('SELECT rate, user_id FROM #__forum_Ratings WHERE topic_id = ' . $cur_topic['id']);
									$num_rows = $db->num_rows ($sql);
									if ($num_rows > 0) {
										$total_rating = 0;
										//Parse data of ratings and user_ids for all ratings of current topic.
										while ($sql_data = $db->fetch_assoc ($sql)) {
											//Should we add the rating to total?
											$flag = 1;
											//Get username of user of specific rating.
											$sql2 = $db->query ('SELECT username FROM #__forum_users WHERE id = ' . $sql_data['user_id']);
											$sql2_data = $db->fetch_assoc ($sql2);
											//Check if user is banned.
											foreach ($yarbb_bans as $ban) {
												if ($sql2_data['username'] == $ban['username'])
													$flag = 0;
											}
											//If user isn't banned, add rating to total.
											if ($flag)
												$total_rating += $sql_data['rate'];
											else
												$num_rows--;
										}
										//If any ratings, calculate the overall rating.
										if ($num_rows > 0) {
											$rating = $total_rating / $num_rows;
											$rating = round ($rating, 2);
											$subject .= '&nbsp;';
											//If the user is not a guest, make the rating clickable to view who voted.
											if (!$yarbb_user['is_guest'])
												$subject .= '<a href="'.YARBB_URL.'&amp;task=rate&amp;v_id=' . $cur_topic['id'] . '">';
											//Rating is rounded to the closest star or half-star.
											//If stars are enabled, find what image file to load.
											if ($rate_stars) {
												if ($rating >= 0.0 && $rating < 0.25)
													$file_name = 'Star_0';
												else if ($rating >= 0.25 && $rating < 0.75)
													$file_name = 'Star_0_Half';
												else if ($rating >= 0.75 && $rating < 1.25)
													$file_name = 'Star_1';
												else if ($rating >= 1.25 && $rating < 1.75)
													$file_name = 'Star_1_Half';
												else if ($rating >= 1.75 && $rating < 2.25)
													$file_name = 'Star_2';
												else if ($rating >= 2.25 && $rating < 2.75)
													$file_name = 'Star_2_Half';
												else if ($rating >= 2.75 && $rating < 3.25)
													$file_name = 'Star_3';
												else if ($rating >= 3.25 && $rating < 3.75)
													$file_name = 'Star_3_Half';
												else if ($rating >= 3.75 && $rating < 4.25)
													$file_name = 'Star_4';
												else if ($rating >= 4.25 && $rating < 4.75)
													$file_name = 'Star_4_Half';
												else
													$file_name = 'Star_5';
												$subject .= '<img src="'.$yarbb_live.'/images/rate_stars/' . $yarbb_config['o_rt_cur_stars'] . '/' . $file_name . '.gif" alt="'.$file_name .'" />';
											}
											//Otherwise, use numbers.
											else {
												$decimal = ($rating - floor ($rating)) * 100;
												$rating = floor ($rating);
												if ($decimal >= 25 && $decimal < 75)
													$rating += 0.5;
												if ($decimal >= 75)
													$rating += 1;
												$subject .= '<strong><small>' . $rating . ' / 5</small></strong>';
											}
											//If the link was made clickable, end the tag.
											if (!$yarbb_user['is_guest'])
												$subject .= '</a>';
										}
									}
								}
								//==================
								//=====END CODE=====
								//==================
								?>
					<tr<?php if ($item_status != '') echo ' class="'.trim($item_status).' '.$rowbg.'"'; else echo ' class="'.$rowbg.'"'; ?>>
						<td class="tcl">
							<div class="intd">
								<div class="<?php echo $icon_type ?>"></div>
								<div class="tclcon">
								<?php echo $subject."\n" ?>
									<br />
								<?php echo yarbb_htmlspecialchars($cur_topic['descrip_t']); ?>
                                </div>
							</div>					</td>
						<td class="tcl"><a href="<?php echo sefRelToAbs(YARBB_URL.'&amp;task=viewforum&amp;id='.($cur_topic['forum_id'])); ?>"><?php echo ($cur_topic['forum_name']) ?></a>&nbsp;</td>
						<td class="tc3"><?php echo ($cur_topic['moved_to'] == null) ? $cur_topic['num_replies'] : '&nbsp;' ?>&nbsp;</td>
						<td class="tc3"><?php echo ($cur_topic['moved_to'] == null) ? $cur_topic['num_views'] : '&nbsp;' ?>&nbsp;</td>
						<td class="ag_tcr"><?php echo $last_post ?></td>
					</tr>
								<?php
							}
						}
						else {
							?>
					<tr>
						<td class="tcl" colspan="7"><?php echo _Y_BANNONE; ?></td>
					</tr>
							<?php
						}
						?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<div class="linksb">
	<div class="inbox">
		<p class="pagelink conl"><?php echo $paging_links ?></p>
	<?php echo $post_link ?>
		<ul><li><a href="<?php echo sefRelToAbs(YARBB_URL) ?>"><?php echo _Y_INDEX; ?></a>&nbsp;</li><li>&raquo;&nbsp;<?php echo _Y_YARALLTOP; ?></li></ul>
		<div class="clearer"></div>
	</div>
</div>
	<?php
}
					?>
